package com.ruoyi.college.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.college.domain.*;
import com.ruoyi.college.mapper.ExperimentScoreItemMapper;
import com.ruoyi.college.mapper.ExperimentScoreLevelMapper;
import com.ruoyi.college.service.ICourseArrangementService;
import com.ruoyi.college.service.ICourseService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.college.mapper.ExperimentScoreStandardMapper;
import com.ruoyi.college.service.IExperimentScoreStandardService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;


/**
 * 设置评分标准Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-27
 */
@Service
public class ExperimentScoreStandardServiceImpl implements IExperimentScoreStandardService 
{
    @Autowired
    private ExperimentScoreStandardMapper experimentScoreStandardMapper;

    @Autowired
    private ICourseArrangementService  courseArrangementService;

    @Autowired
    private ICourseService courseService;

    @Resource
    private ExperimentScoreStandardMapper scoreStandardMapper;

    @Resource
    private ExperimentScoreItemMapper scoreItemMapper;

    @Resource
    private ExperimentScoreLevelMapper scoreLevelMapper;

    /**
     * 当前登录用户
     */
    private String getUsername() {
        //return SecurityUtils.getUsername();
        return "";
    }

    @Override
    public ExperimentScoreStandard selectByCourseId(Long courseId) {
        ExperimentScoreStandard standard = scoreStandardMapper.selectByCourseId(courseId);
        if (standard != null) {
            // 查询评分项
            List<ExperimentScoreItem> items = scoreItemMapper.selectByStandardId(standard.getStandardId());
            if (items != null && !items.isEmpty()) {
                // 查询每个评分项的评分等级
                for (ExperimentScoreItem item : items) {
                    List<ExperimentScoreLevel> levels = scoreLevelMapper.selectByItemId(item.getStandardId());
                    item.setScoreLevels(levels);
                }
                standard.setScoreItems(items);
            }
        }
        return standard;
    }

    @Autowired
    private ExperimentScoreItemMapper experimentScoreItemMapper;
    /**
     * 查询设置评分标准
     * 
     * @param standardId 设置评分标准主键
     * @return 设置评分标准
     */
    @Override
    public ExperimentScoreStandard selectExperimentScoreStandardByStandardId(Long standardId)
    {
        ExperimentScoreStandard experimentScoreStandard = experimentScoreStandardMapper.selectExperimentScoreStandardByStandardId(standardId);
        List<ExperimentScoreItem> experimentScoreItems = experimentScoreItemMapper.selectByStandardId(standardId);
        if (experimentScoreItems != null && !experimentScoreItems.isEmpty()) {
            for (ExperimentScoreItem experimentScoreItem : experimentScoreItems) {
                List<ExperimentScoreLevel> levels = scoreLevelMapper.selectByItemId(experimentScoreItem.getItemId());
                experimentScoreItem.setScoreLevels(levels);
            }
            experimentScoreStandard.setScoreItems(experimentScoreItems);
        }
        Long arrangementId = experimentScoreStandard.getArrangementId();
        CourseArrangement courseArrangement = courseArrangementService.selectCourseArrangementByArrangementId(arrangementId);
        Long courseId = courseArrangement.getCourseId();
        Course course = courseService.selectCourseByCourseId(courseId);
        experimentScoreStandard.setCourseId(courseId);
        experimentScoreStandard.setCourseName(course.getCourseName());
        return experimentScoreStandard;
    }

    /**
     * 查询设置评分标准列表
     * 
     * @param experimentScoreStandard 设置评分标准
     * @return 设置评分标准
     */
    @Override
    public List<ExperimentScoreStandard> selectExperimentScoreStandardList(ExperimentScoreStandard experimentScoreStandard)
    {
        List<ExperimentScoreStandard> experimentScoreStandards = experimentScoreStandardMapper.selectExperimentScoreStandardList(experimentScoreStandard);
        experimentScoreStandards.forEach(item->{
            Long arrangementId = item.getArrangementId();
            CourseArrangement courseArrangement = courseArrangementService.selectCourseArrangementByArrangementId(arrangementId);
            Long courseId = courseArrangement.getCourseId();
            Course course = courseService.selectCourseByCourseId(courseId);
            item.setCourseId(courseId);
            item.setCourseName(course.getCourseName());
        });
        return experimentScoreStandards;
    }

    /**
     * 新增设置评分标准
     * 
     * @param experimentScoreStandard 设置评分标准
     * @return 结果
     */
    @Override
    public int insertExperimentScoreStandard(ExperimentScoreStandard experimentScoreStandard)
    {
        experimentScoreStandard.setCreateTime(DateUtils.getNowDate());
        return experimentScoreStandardMapper.insertExperimentScoreStandard(experimentScoreStandard);
    }

    /**
     * 修改设置评分标准
     * 
     * @param experimentScoreStandard 设置评分标准
     * @return 结果
     */
    @Override
    public int updateExperimentScoreStandard(ExperimentScoreStandard experimentScoreStandard)
    {
        experimentScoreStandard.setUpdateTime(DateUtils.getNowDate());
        return experimentScoreStandardMapper.updateExperimentScoreStandard(experimentScoreStandard);
    }

    /**
     * 批量删除设置评分标准
     * 
     * @param standardIds 需要删除的设置评分标准主键
     * @return 结果
     */
    @Override
    public int deleteExperimentScoreStandardByStandardIds(String standardIds)
    {
        return experimentScoreStandardMapper.deleteExperimentScoreStandardByStandardIds(Convert.toStrArray(standardIds));
    }

    /**
     * 删除设置评分标准信息
     * 
     * @param standardId 设置评分标准主键
     * @return 结果
     */
    @Override
    public int deleteExperimentScoreStandardByStandardId(Long standardId)
    {
        return experimentScoreStandardMapper.deleteExperimentScoreStandardByStandardId(standardId);
    }

    @Override
    public List<ExperimentScoreStandard> selectByLeader() {
        //

        return scoreStandardMapper.selectByLeader(getUsername());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveScoreStandard(ExperimentScoreStandard scoreStandard) {
        //String username = getUsername();
        Date now = new Date();

        // 设置公共字段
        //scoreStandard.setUpdateBy(username);
        scoreStandard.setUpdateTime(now);

        //if (scoreStandard.getStandardId() == null) {
        //    // 新增评分标准
        //    //scoreStandard.setCreateBy(username);
        //    scoreStandard.setCreateTime(now);
        //    scoreStandardMapper.insertExperimentScoreStandard(scoreStandard);
        //} else {
        //    // 更新评分标准，先删除原有的评分项和评分等级
        //    scoreLevelMapper.deleteByStandardId(scoreStandard.getStandardId());
        //    scoreItemMapper.deleteByStandardId(scoreStandard.getStandardId());
        //    scoreStandardMapper.updateExperimentScoreStandard(scoreStandard);
        //}

        //List<ExperimentScoreItem> experimentScoreItems = scoreItemMapper.selectByStandardId(scoreStandard.getStandardId());
        //if (experimentScoreItems != null && !experimentScoreItems.isEmpty()) {
        //    for (ExperimentScoreItem experimentScoreItem : experimentScoreItems) {
        //        List<ExperimentScoreLevel> levels = scoreLevelMapper.selectByItemId(experimentScoreItem.getItemId());
        //        if (levels != null && !levels.isEmpty()) {
        //            for (ExperimentScoreLevel level : levels) {
        //                scoreLevelMapper.deleteByLevelId(level.getLevelId());
        //            }
        //        }
        //    }
        //}
        if (scoreStandard.getStandardId() != null) {
            List<ExperimentScoreItem> experimentScoreItems = scoreItemMapper.selectByStandardId(scoreStandard.getStandardId());
            if (experimentScoreItems != null && !experimentScoreItems.isEmpty()) {
                scoreLevelMapper.deleteByStandardId(scoreStandard.getStandardId());
                scoreItemMapper.deleteByStandardId(scoreStandard.getStandardId());
            }
        }

        // 保存评分项和评分等级
        List<ExperimentScoreItem> items = scoreStandard.getScoreItems();
        if (items != null && !items.isEmpty()) {
            for (int i = 0; i < items.size(); i++) {
                ExperimentScoreItem item = items.get(i);
                item.setStandardId(scoreStandard.getStandardId());
                item.setSort(i + 1);
                //item.setCreateBy(username);
                item.setCreateTime(now);
                //item.setUpdateBy(username);
                item.setUpdateTime(now);
                // 保存评分项
                scoreItemMapper.insertExperimentScoreItem(item);

                Long itemId = item.getItemId();
                // 保存评分等级
                List<ExperimentScoreLevel> levels = item.getScoreLevels();
                if (levels != null && !levels.isEmpty()) {
                    for (int j = 0; j < levels.size(); j++) {
                        ExperimentScoreLevel level = levels.get(j);
                        level.setItemId(itemId);
                        level.setSort(j + 1);
                        //level.setCreateBy(username);
                        level.setCreateTime(now);
                        //level.setUpdateBy(username);
                        level.setUpdateTime(now);
                        scoreLevelMapper.insertExperimentScoreLevel(level);
                    }
                }
            }
        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteScoreStandard(Long id) {
        // 级联删除评分等级、评分项、评分标准
        scoreLevelMapper.deleteByStandardId(id);
        scoreItemMapper.deleteByStandardId(id);
        return scoreStandardMapper.deleteExperimentScoreStandardByStandardId(id);
    }
}
